package com.tg.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.tg.app.R;
import com.tg.app.activity.device.add.AddFragmentTabActivity;
import com.tange.base.toolkit.WifiAp;


import java.util.ArrayList;

public class WifiListAdapter extends BaseAdapter {
    private ArrayList<String> list;
    private Context context;
    private OnAddWifiDeviceClickListener listener;
    private int originType;
    private boolean isApAdd;

    public WifiListAdapter(ArrayList<String> list,Context context,OnAddWifiDeviceClickListener listener,int originType){
        this.list = list;
        this.context = context;
        this.listener = listener;
        this.originType = originType;
        isApAdd = originType == AddFragmentTabActivity.DEVICE_ADD_TYPE_AP || originType == AddFragmentTabActivity.DEVICE_ADD_TYPE_AP_LOCAL;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        WifiListAdapter.ViewHolder holder;
        if(convertView == null){
            holder = new WifiListAdapter.ViewHolder();
            if (isApAdd) {
                convertView = LayoutInflater.from(context).inflate(R.layout.ap_wifi_list_item,null);
            } else {
                convertView = LayoutInflater.from(context).inflate(R.layout.lan_wifi_list_item,null);
            }
            holder.item = convertView.findViewById(R.id.item_resolution);
            convertView.setTag(holder);
        }else{
            holder = (WifiListAdapter.ViewHolder)convertView.getTag();
        }

        if (!isApAdd){
            holder.addWifiDeviceTextView = convertView.findViewById(R.id.add_wifi_device_text_view);
            holder.addWifiDeviceTextView.setOnClickListener(new View.OnClickListener() {
                @Override

                public void onClick(View v) {
                    listener.onAddDeviceClick(position);
                }
            });
            holder.item.setText(list.get(position));
        }else {
            WifiAp wifiAp = JSON.parseObject(list.get(position), WifiAp.class);
            holder.item.setText(wifiAp.ssid);

        }


        return convertView;
    }

    public final class ViewHolder
    {
         TextView item,addWifiDeviceTextView;
    }

    public interface OnAddWifiDeviceClickListener{
        void onAddDeviceClick(int position);
    }
}
